<template>
  <a-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleCancel"
    @cancel="handleCancel"
  >
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="0">
          <a-col :md="24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              style="float: right;margin-bottom: 20px;"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}">
              <a-button
                class="btn_add"
                type="primary"
                @click="$refs.selectForm.init(sceneId)"
                icon="plus">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.id"
    >
      <span slot="configName" slot-scope="text,record">
        <a-tooltip>
          <template v-if="!!record.remark" slot="title">{{ record.remark }}</template>
          {{ text }}
        </a-tooltip>
      </span>
      <span slot="status" slot-scope="text">
        {{ statusFliter(text) }}
      </span>
      <span slot="action" slot-scope="text, record">
        <a class="btn_edit" @click="$refs.editConfigForm.edit(record)">编辑</a>
        <a-divider type="vertical"/>
        <a-popconfirm class="btn_del" placement="topRight" title="确认删除？" @confirm="() => del(record)">
          <a>删除</a>
        </a-popconfirm>
      </span>
    </s-table>
    <select-form ref="selectForm" @ok="handleAddOk"/>
    <edit-config-form ref="editConfigForm" @ok="handleOk"/>
  </a-modal>
</template>
<script>

  import { pageTemplate, deleteTemplate } from '@/api/modular/main/gatewayscene/gatewayscene'
  import { STable } from '@/components'
  import selectForm from './selectForm'
  import editConfigForm from './editConfigForm'

  export default {
    components: {
      STable,
      selectForm,
      editConfigForm
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        sceneId: 0,
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          this.queryParam.id = this.sceneId
          return pageTemplate(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {},
        columns: [
          // 表头
          {
            title: '名称',
            dataIndex: 'configName',
            scopedSlots: { customRender: 'configName' }
          },
          {
            title: '编码',
            dataIndex: 'code'
          },
          {
            title: '默认值',
            dataIndex: 'value'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 100,
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        title: ''
      }
    },
    methods: {
      init (record) {
        this.title = '场景[' + record.sceneName + ']配置'
        this.sceneId = record.id
        this.visible = true
        this.queryParam = {}
        try {
          this.$refs.table.refresh(true)
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      },
      del (record) {
        deleteTemplate({
          templateId: record.id
        })
          .then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('删除失败：' + res.message)
            }
          })
          .catch((err) => {
            this.$message.error('删除错误：' + err.message)
          })
      },
      statusFliter (text) {
        if (text === 1) {
          return '启用'
        } else if (text === 0) {
          return '禁用'
        }
      },
      handleCancel () {
        this.form.resetFields()
        this.queryParam = {}
        this.visible = false
      },
      handleOk () {
        this.$refs.table.refresh(true)
      },
      handleAddOk () {
        this.$refs.table.refresh(true)
      }
    }
  }
</script>
